#!/bin/bash

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/release_lib.sh"

CONTROL=debian/control
CHLOG=debian/changelog
CHDATE=$(LC_ALL=C date -R | sed -e 's/,/\\,/g') # Replace comma to '\,'

# Build a package with configuration files for Yandex.Metrika.
BUILD_PACKAGE_FOR_METRIKA=$([ -f 'private/Server/metrika/config.xml' ] && echo 'yes')

while [[ $1 == --* ]]
do
	# Don't sign packages, don't upload, don't create tags.
	if [[ $1 == '--standalone' ]]
	then
		STANDALONE='yes'
		DEBUILD_NOSIGN_OPTIONS="-us -uc"
		shift
	elif [[ $1 == '--test' ]]
	then
		STANDALONE='yes'
		TEST='yes'
		DEBUILD_NOSIGN_OPTIONS="-us -uc"
		shift
	elif [[ $1 == '--ignore-deps' ]]
	then
		DEBUILD_NODEPS_OPTIONS="-d"
		shift
	else
		echo "Unknown option $1"
		exit 2
	fi
done

# List of daemons to build could be specified in command line arguments.
if [ $# -gt 0 ]
then
	DAEMONS="$@"
else
	DAEMONS="$(echo `cat debian/daemons`)"
fi

if [[ $TEST != 'yes' ]]
then
	gen_revision_author
else
	REVISION=99999
fi

echo -e "\nCurrent revision is $REVISION"

make_control "$CONTROL" "$DAEMONS"

gen_changelog "$REVISION" "$CHDATE" "$AUTHOR" "$CHLOG" "$DAEMONS"

# Build (only binary packages).
debuild -e DAEMONS="${DAEMONS}" -e ENABLE_MONGODB -e GLIBC_COMPATIBILITY -e CC -e CXX -b ${DEBUILD_NOSIGN_OPTIONS} ${DEBUILD_NODEPS_OPTIONS}

if [[ $STANDALONE != 'yes' ]]
then
	upload_debs "$REVISION" "$DAEMONS"
fi
